package com.zhixin.fans.ks.ui.main.mutual;

import android.Manifest;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.PermissionUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import com.zhixin.fans.ks.R;

import java.util.ArrayList;
import java.util.List;




/**
 * Created by Eenie on 2017/6/2.
 * Email: eeniework@gmail.com
 * Des:
 */

public class UploadImgAdapter extends BaseQuickAdapter<UploadImg, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener {

    int maxCount;
    UploadImg mEmptyImg = new UploadImg(1);
    OnItemViewClickListener mOnItemViewClickListener;

    public UploadImgAdapter(int maxCount) {
        super(R.layout.item_upload_img, new ArrayList<>());
        this.maxCount = maxCount;
        setOnItemClickListener(this);


        getData().add(mEmptyImg);
        notifyDataSetChanged();

    }

    @Override
    protected void convert(final BaseViewHolder helper, UploadImg item) {
        ImageView iv = helper.getView(R.id.img_pic);
        if (item.getType() == 1) {
            Glide.with(mContext).load(R.mipmap.ic_plus_pic).into(iv);
        } else {
            Glide.with(mContext).load(item.getPath()).into(iv);
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UploadImg uploadImg = (UploadImg) adapter.getItem(position);
        if (uploadImg.getType() == 1) {
            if (checkPermission()) {
                if (mOnItemViewClickListener != null) {
                    mOnItemViewClickListener.onEmptyClick();
                }
            } else {
                requestPermission();
            }
        } else {
            if (mOnItemViewClickListener != null) {
                mOnItemViewClickListener.onItemClick(uploadImg);
            }
        }
    }

    public void setOnItemViewClickListener(OnItemViewClickListener onItemViewClickListener) {
        mOnItemViewClickListener = onItemViewClickListener;
    }

    private boolean checkPermission() {
        return PermissionUtils.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
    }

    private boolean requestPermission() {
        PermissionUtils.permission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                .callback(new PermissionUtils.FullCallback() {
                    @Override
                    public void onGranted(List<String> permissionsGranted) {
                        if (mOnItemViewClickListener != null) {
                            mOnItemViewClickListener.onEmptyClick();
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {

                    }
                })
                .request();

        return PermissionUtils.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA);
    }



    public interface OnItemViewClickListener {
        void onEmptyClick();

        void onItemClick(UploadImg uploadImg);
    }


    @Override
    public void addData(@NonNull UploadImg data) {
        if (getData().size() < maxCount + 1) {
            addData(0, data);
        }
        if (getData().size() > maxCount) {
            getData().remove(mEmptyImg);
        }
        notifyDataSetChanged();
    }


}
